import { ReactWidget, ApplicationShell, WidgetManager } from '@theia/core/lib/browser';
import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
import * as React from '@theia/core/shared/react';
import { MenuPath } from '@theia/core';
import { Message } from '@theia/core/lib/browser';
import { OrangeWidgetWidget } from 'orange-widget/lib/browser/orange-widget-widget';

export const APPLE_CONTEXT_MENU: MenuPath = ['theia-examples:apple-context-menu'];

/** Implementation of the Tree Widget */
@injectable()
export class AppleWidget extends ReactWidget {
    /** The ID of the view */
    static readonly ID = 'apple-widget:view';
    /** The label of the view */
    static readonly LABEL = 'Apple Widget';

    @inject(ApplicationShell) protected readonly shell: ApplicationShell;
    @inject(WidgetManager) protected readonly widgetManager: WidgetManager;

    @postConstruct()
    protected init(): void {
        this.doInit();
    }

    protected async doInit(): Promise<void> {
        this.id = AppleWidget.ID;
        this.title.label = AppleWidget.LABEL;
        this.title.caption = AppleWidget.LABEL;
        this.title.closable = true;
        this.title.iconClass = 'fa fa-smile-o';
        this.update();
    }

    protected async showOtherWidget(): Promise<void> {
        const w = await this.widgetManager.getOrCreateWidget(OrangeWidgetWidget.ID);
        if (w) {
            this.shell.mainPanel.addWidget(w);
        }
    }

    protected override render(): React.ReactNode {
        return (
            <div>
                <div>Hello, World!</div>
                <button onClick={() => this.showOtherWidget()}>
                    显示其它widget
                </button>
            </div>
        );
    }

    protected onActivateRequest(msg: Message): void {
        super.onActivateRequest(msg);
    }
}
